package com.sgb.kjwl.view.ui.activity.leaseAsk;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAskLeaseMyPriceDetailBinding;
import com.sgb.kjwl.model.entity.user.PublicMyPriceListEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseQuedPriceEvent;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.viewmodel.machine.AskLeaseMyPriceDetailModel;
import com.sgb.merchant.BR;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

/**
 * 租赁-报价详情
 */
public class AskLeaseMyPriceDetailActivity extends AppActivity {

    private ActivityAskLeaseMyPriceDetailBinding binding;
    private AskLeaseMyPriceDetailModel viewModel;
    private PublicMyPriceListEntity entity;
    private String type = "0";//0，我的报价，1客户报价

    public static void start(Context context, PublicMyPriceListEntity entity, String type) {
        Intent intent = new Intent();
        intent.putExtra("PublicMyPriceListEntity", entity);
        intent.putExtra("type", type);
        intent.setClass(context, AskLeaseMyPriceDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_ask_lease_my_price_detail);
        viewModel = new ViewModelProvider(this).get(AskLeaseMyPriceDetailModel.class);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("报价详情");
        binding.tvHuanxun.setOnClickListener(v -> MToast.showToast(AskLeaseMyPriceDetailActivity.this, "功能开发中"));
        binding.tvJiaoliu.setOnClickListener(v -> {
            if ("0".equals(type)) {
                if(TextUtils.isEmpty(entity.getDemandCreateUserNo())){
                    MToast.showToast(AskLeaseMyPriceDetailActivity.this, "未设置交流账号!");
                    return;
                }
                viewModel.getImByCustomerId(entity.getDemandCreateUserNo());
            } else {
                if(TextUtils.isEmpty(entity.getCreateUser())){
                    MToast.showToast(AskLeaseMyPriceDetailActivity.this, "未设置交流账号!");
                    return;
                }
                viewModel.getImByCustomerId(entity.getCreateUser());
            }
        });
        binding.tvAccept.setOnClickListener(v -> {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("提示")
                    .setMsg("确定接受该报价吗？确定后报价状态变成已接受")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.acceptPrice(entity.getRentalSeekId(), entity.getId(), entity.getCategoryType());
                        }

                        @Override
                        public void onLeftClick() {
                        }
                    }).show();

        });
        binding.tvCancel.setOnClickListener(v -> {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("提示")
                    .setMsg("确定取消该报价吗？确定后报价状态变成已取消")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.cancelPrice(entity.getId());
                        }

                        @Override
                        public void onLeftClick() {
                        }
                    }).show();
        });

        binding.tvLookDetail.setOnClickListener(v -> {
            //查看需求-H5
            if ("1".equals(entity.getDemandStatus()) && !"1".equals(entity.getIsAcceptQuote())) {
                MToast.showToast(getActivity(), "该需求已被发布者删除");
                return;
            }
            if ("1".equals(type)) {//客户报价
                viewModel.getMyH5Url(entity.getCategoryType(), entity.getRentalSeekId());
            } else {//我的报价
                viewModel.getH5Url(entity.getCategoryType(), entity.getRentalSeekId());
            }
        });
    }

    private void receiverLiveData() {
        viewModel.showStateAcceptLiveData.observe(this, data -> {
            MToast.showToast(getActivity(), data);
            binding.tvAccept.setVisibility(View.GONE);
            binding.tvStatusName.setText("已接受");
            binding.tvStatusName.setTextColor(Color.parseColor("#06C764"));
            //发消息到列表刷新数据
            EventBus.getDefault().postSticky(LeaseQuedPriceEvent.make().setMessageTag(EventConstant.EVENT_MY_PRICE_LIST));
        });
        viewModel.showStateCancelLiveData.observe(this, data -> {
            MToast.showToast(getActivity(), data);
            binding.tvCancel.setVisibility(View.GONE);
            binding.tvStatusName.setText("已取消");
            binding.tvStatusName.setTextColor(Color.parseColor("#969696"));
            //发消息到列表刷新数据
            EventBus.getDefault().postSticky(LeaseQuedPriceEvent.make().setMessageTag(EventConstant.EVENT_MY_PRICE_LIST));
        });
        viewModel.showMyUrlLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String url) {
                WebViewX5HostActivity.start(AskLeaseMyPriceDetailActivity.this, url);
            }
        });
        viewModel.showUrlLiveData.observe(this, new Observer<String[]>() {
            @Override
            public void onChanged(String[] strings) {
                WebViewX5HostActivity.start(AskLeaseMyPriceDetailActivity.this, strings[0], strings[1], "asklease", strings[2]);
            }
        });
    }

    @Override
    protected void init() {
        receiverLiveData();
        if (getIntent() != null) {
            type = getIntent().getStringExtra("type");
            entity = (PublicMyPriceListEntity) getIntent().getSerializableExtra("PublicMyPriceListEntity");

            if (entity != null) {
                binding.setVariable(BR.viewModel, entity);

                if ("0".equals(type)) {//我的报价
                    binding.tvDemandNameLabel.setText("需求方名称");
                    binding.tvDemandName.setText(entity.getDemandCompanyName());
                    binding.llContactName.setVisibility(View.VISIBLE);
                } else {
                    binding.tvDemandNameLabel.setText("客户名称");
                    binding.tvDemandName.setText(entity.getCompanyName());
                    binding.llContactName.setVisibility(View.GONE);
                    if ("2".equals(entity.getQuoteType())) {//报价身份为个人，不显示报价人
                        binding.llContactName.setVisibility(View.GONE);
                    } else {
                        binding.llContactName.setVisibility(View.VISIBLE);
                    }
                }

                //处理状态 （0待接受，1已接受，2已忽略，3已取消）
                if ("0".equals(entity.getIsAcceptQuote())) {//待接受
                    binding.tvStatusName.setTextColor(Color.parseColor("#FF9A3C"));
                    if ("0".equals(type)) {//我的报价
                        binding.tvCancel.setVisibility(View.VISIBLE);
                        binding.tvAccept.setVisibility(View.GONE);
                    } else {//客户报价
                        binding.tvCancel.setVisibility(View.GONE);
                        binding.tvAccept.setVisibility(View.VISIBLE);
                    }
                    if (entity.getStatus() == 3) {//截止时间已到，为报价已结束
                        binding.tvCancel.setVisibility(View.GONE);
                        binding.tvAccept.setVisibility(View.GONE);
                    } else if (entity.getStatus() == 4) {//进场时间已到，为报价已完成
                        binding.tvCancel.setVisibility(View.GONE);
                        binding.tvAccept.setVisibility(View.GONE);
                    }
                } else if ("1".equals(entity.getIsAcceptQuote())) {//已接受
                    binding.tvStatusName.setTextColor(Color.parseColor("#06C764"));
                    binding.tvCancel.setVisibility(View.GONE);
                    binding.tvAccept.setVisibility(View.GONE);
                } else if ("3".equals(entity.getIsAcceptQuote())) {//已取消
                    binding.tvStatusName.setTextColor(Color.parseColor("#969696"));
                    binding.tvCancel.setVisibility(View.GONE);
                    binding.tvAccept.setVisibility(View.GONE);
                    if ("0".equals(type)) {//我的报价在已取消状态下不显示交流函询
                        binding.tvHuanxun.setVisibility(View.GONE);
                        binding.tvJiaoliu.setVisibility(View.GONE);
                    }
                }

                //区分面积和计量单位（0）
                if ("2".equals(entity.getCategoryType())) {
                    binding.tvModelLabel.setText("车系");
                }
                if ("3".equals(entity.getCategoryType()) || "4".equals(entity.getCategoryType()) || "5".equals(entity.getCategoryType())) {
                    binding.llBrand.setVisibility(View.GONE);
                    binding.llModel.setVisibility(View.GONE);
                    binding.tvNumLabel.setText("面积");
                    binding.tvNum.setText(TextUtils.isEmpty(entity.getRentalArea()) ? "暂无信息" : entity.getRentalArea() + "m²");
                } else {
                    binding.tvNumLabel.setText("计量单位");
                    binding.tvNum.setText(entity.getRentalCountUnit());
                }

            }
        }
    }

}
